{% extends 'base.html' %}

{% block title %}借阅《{{ book.title }}》 - 图书馆管理系统{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0">
                        <i class="fas fa-hand-holding me-2"></i>借阅书籍
                    </h3>
                    <a href="{% url 'books:book_detail' book.id %}" class="btn btn-outline-secondary btn-sm">
                        <i class="fas fa-arrow-left me-1"></i>返回详情
                    </a>
                </div>
            </div>
            <div class="card-body">
                <!-- 书籍信息 -->
                <div class="row mb-4">
                    <div class="col-md-8">
                        <h5>书籍信息</h5>
                        <table class="table table-borderless">
                            <tr>
                                <td><strong>书名：</strong></td>
                                <td>{{ book.title }}</td>
                            </tr>
                            <tr>
                                <td><strong>作者：</strong></td>
                                <td>{{ book.author }}</td>
                            </tr>
                            <tr>
                                <td><strong>ISBN：</strong></td>
                                <td>{{ book.isbn }}</td>
                            </tr>
                            <tr>
                                <td><strong>分类：</strong></td>
                                <td>{{ book.category.name }}</td>
                            </tr>
                            <tr>
                                <td><strong>可借数量：</strong></td>
                                <td>
                                    <span class="badge bg-success">{{ book.available_copies }}/{{ book.total_copies }}</span>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-4">
                        <div class="alert alert-info">
                            <h6><i class="fas fa-info-circle me-1"></i>借阅须知</h6>
                            <ul class="mb-0 small">
                                <li>借阅期限最长90天</li>
                                <li>请按时归还书籍</li>
                                <li>逾期将影响借阅权限</li>
                                <li>爱护书籍，保持整洁</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- 借阅表单 -->
                <form method="post">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.due_date.id_for_label }}" class="form-label">
                                    <i class="fas fa-calendar me-1"></i>{{ form.due_date.label }}
                                </label>
                                {{ form.due_date }}
                                {% if form.due_date.help_text %}
                                <div class="form-text">{{ form.due_date.help_text }}</div>
                                {% endif %}
                                {% if form.due_date.errors %}
                                <div class="text-danger">
                                    {% for error in form.due_date.errors %}
                                    <small>{{ error }}</small>
                                    {% endfor %}
                                </div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.notes.id_for_label }}" class="form-label">
                                    <i class="fas fa-sticky-note me-1"></i>{{ form.notes.label }}
                                </label>
                                {{ form.notes }}
                                {% if form.notes.errors %}
                                <div class="text-danger">
                                    {% for error in form.notes.errors %}
                                    <small>{{ error }}</small>
                                    {% endfor %}
                                </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-check me-1"></i>确认借阅
                        </button>
                        <a href="{% url 'books:book_detail' book.id %}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-1"></i>取消
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- 借阅历史 -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <i class="fas fa-history me-2"></i>我的借阅历史
                </h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>书籍</th>
                                <th>借阅日期</th>
                                <th>应还日期</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in user.borrowrecord_set.all|slice:":5" %}
                            <tr>
                                <td>{{ record.book.title }}</td>
                                <td>{{ record.borrow_date|date:"Y-m-d" }}</td>
                                <td>{{ record.due_date|date:"Y-m-d" }}</td>
                                <td>
                                    {% if record.status == 'borrowed' %}
                                        {% if record.is_overdue %}
                                        <span class="badge bg-danger">逾期</span>
                                        {% else %}
                                        <span class="badge bg-warning">借阅中</span>
                                        {% endif %}
                                    {% elif record.status == 'returned' %}
                                    <span class="badge bg-success">已归还</span>
                                    {% else %}
                                    <span class="badge bg-secondary">{{ record.get_status_display }}</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if record.status == 'borrowed' %}
                                    <a href="{% url 'books:return_book' record.id %}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-undo me-1"></i>归还
                                    </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="5" class="text-center text-muted">
                                    <i class="fas fa-inbox fa-2x mb-2"></i>
                                    <p>暂无借阅记录</p>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% if user.borrowrecord_set.count > 5 %}
                <div class="text-center">
                    <a href="{% url 'books:my_borrows' %}" class="btn btn-outline-primary">
                        <i class="fas fa-list me-1"></i>查看全部借阅记录
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %} 